package com.ywjh.inter.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FileOp {

	public static String readContent(File file) {
		BufferedReader br = null;
		StringBuffer sb = new StringBuffer();
		try {
			br = new BufferedReader(new FileReader(file));
			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {

					br.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

	public static boolean writeContent(String content, File file) {
		BufferedWriter bw = null;
		boolean b = false;
		try {
			bw = new BufferedWriter(new FileWriter(file, true));
			bw.write(content);
			bw.newLine();
			b = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return b;
	}

	public static boolean writeContent(String content, File file, boolean append) {
		BufferedWriter bw = null;
		boolean b = false;
		try {
			bw = new BufferedWriter(new FileWriter(file, append));
			bw.write(content);
			bw.newLine();
			b = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return b;
	}

	public static List<String> readContent1(File file) {
		BufferedReader br = null;
		List<String> sb = new ArrayList<String>();
		try {
			br = new BufferedReader(new FileReader(file));
			String line = "";
			while ((line = br.readLine()) != null) {
				// sb.append(line);
				sb.add(line);
			}
		} catch (FileNotFoundException e) {
			// e.printStackTrace();
		} catch (IOException e) {
			// e.printStackTrace();
		} finally {
			if (br != null) {
				try {

					br.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return sb;
	}

	public static Set<String> readContent2(File file) {
		BufferedReader br = null;
		Set<String> sb = new HashSet<String>();
		try {
			br = new BufferedReader(new FileReader(file));
			String line = "";
			while ((line = br.readLine()) != null) {
				// sb.append(line);
				sb.add(line);
			}
		} catch (FileNotFoundException e) {
			// e.printStackTrace();
		} catch (IOException e) {
			// e.printStackTrace();
		} finally {
			if (br != null) {
				try {

					br.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return sb;
	}

	public static boolean writeContent(Set<String> set, File file) {
		BufferedWriter bw = null;
		boolean b = false;
		try {
			bw = new BufferedWriter(new FileWriter(file, true));
			for (String content : set) {
				bw.write(content);
				bw.newLine();
			}
			b = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return b;
	}

	public static boolean writeContent(List<String> set, File file) {
		BufferedWriter bw = null;
		boolean b = false;
		try {
			bw = new BufferedWriter(new FileWriter(file, true));
			for (String content : set) {
				bw.write(content);
				bw.newLine();
			}
			b = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return b;
	}
}
